﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Text;
using SurveySystem.Core;

namespace SurveySystem.Web
{
    public partial class PublishSurvey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                RedirectDefault();

            if (!IsPostBack)
            {
                InitControls();
            }

           
            
        }

        private void InitControls()
        {
            calendar.SelectedDate = DateTime.Now;
            for (int i = 0; i < 24; i++)
            {
                DropDownList_Hours.Items.Add(i.ToString());
            }
            DropDownList_Hours.SelectedValue = DateTime.Now.Hour.ToString();
            for (int i = 0; i < 60; i++)
            {
                DropDownList_Minutes.Items.Add(i.ToString());
            }
            DropDownList_Minutes.SelectedValue = DateTime.Now.Minute.ToString();
        }

        private SurveyType SurveyType
        {
            get
            {
                if (rbOneChoice.Checked)
                    return SurveyType.Choice;
                else if (rbMultiChoice.Checked)
                    return SurveyType.MultiChoice;
                else
                    return SurveyType.Free;
            }
        }

        private void RedirectDefault()
        {
            Response.Redirect("~/Default.aspx");
        }

        protected void btnStep0Complete_Click(object sender, EventArgs e)
        {
            if (rbPublishToGroups.Checked)
                publishWizard.ActiveViewIndex = 1;
            else if (rbPublishToUsers.Checked)
            {
                publishWizard.ActiveViewIndex = 2;

            }
            else
            {
                if (SurveyType == SurveyType.Free)
                    publishWizard.ActiveViewIndex = 4;
                else
                    publishWizard.ActiveViewIndex = 3;
            }
        }



        protected void returnToStep0(object sender, EventArgs e)
        {
            publishWizard.ActiveViewIndex = 0;
        }

        protected void groupsOfUserDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["userName"] = User.Identity.Name;
        }

        protected void btnFilterAll_Click(object sender, EventArgs e)
        {
            gridView_GroupsOfUsers.DataBind();
        }

        protected void btnDeleteSelectedGroup_Click(object sender, EventArgs e)
        {
            if (lbSelectedGroups.SelectedIndex > -1)
                lbSelectedGroups.Items.RemoveAt(lbSelectedGroups.SelectedIndex);
        }

        protected void gridView_GroupsOfUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "SelectGroup")
            {
                int rowIdx = Convert.ToInt32(e.CommandArgument);
                int groupId = (int)gridView_GroupsOfUsers.DataKeys[rowIdx].Value;
                string groupName = gridView_GroupsOfUsers.Rows[rowIdx].Cells[0].Text;
                ListItem li = new ListItem(Server.HtmlDecode(groupName), groupId.ToString());
                if (!lbSelectedGroups.Items.Contains(li))
                    lbSelectedGroups.Items.Add(li);

            }
        }

        protected IEnumerable<int> GetSelectedGroups()
        {
            return from li in lbSelectedGroups.Items.Cast<ListItem>()
                   select Convert.ToInt32(li.Value);
        }

        protected IEnumerable<Guid> GetSelectedUsers()
        {
            return from li in lbUsers.Items.Cast<ListItem>()
                   select new Guid(li.Value);
        }

        protected void btnSelectGroupsComplete_Click(object sender, EventArgs e)
        {
            if (SurveyType != SurveyType.Free)
                publishWizard.ActiveViewIndex = 3;
            else
                publishWizard.ActiveViewIndex = 4;
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            RedirectDefault();
        }

        protected void btnCreateSurvey_Click(object sender, EventArgs e)
        {
            //foreach (int groupId in GetSelectedGroups())
            //{
            //    Debug.Print(groupId.ToString());
            //}
            DateTime expTime = calendar.SelectedDate;
            expTime = expTime.Date;
            expTime = expTime.AddHours(Convert.ToDouble(DropDownList_Hours.SelectedValue));
            expTime = expTime.AddMinutes(Convert.ToDouble(DropDownList_Minutes.SelectedValue));

            if (expTime < DateTime.Now)
            {
                lblDateError.Visible = true;
               
                
            }
            else
            {
                SurveyConfiguration surveyConfig = new SurveyConfiguration()
                {
                    creatorName = User.Identity.Name,
                    expirationTime = expTime,
                    question = tbQuestion.Text,
                    sendEmailOnCreation = chkEmailOnCreation.Checked,
                    sendEmailOnExpiration = chkEmailOnExpiration.Checked,
                    sendSmsOnCreation = chkSmsOnCreation.Checked,
                    sendSmsOnExpiration = chkSmsOnExpiration.Checked,
                    surveyType = this.SurveyType
                };

                IEnumerable<string> choices = lbChoices.Items.Cast<ListItem>().Select(li => li.Text);

                if (rbPublishToEverybody.Checked)
                {
                    SurveysBLL.PublishNewOpenSurvey(surveyConfig, choices);
                }
                else if (rbPublishToGroups.Checked)
                {
                    SurveysBLL.PublishNewClosedSurvey(surveyConfig, choices, null, GetSelectedGroups());
                }
                else
                {
                    SurveysBLL.PublishNewClosedSurvey(surveyConfig, choices, GetSelectedUsers(), null);
                }

                publishWizard.ActiveViewIndex = 5;
            }
            
        }

        protected void finish_Activate(object sender, EventArgs e)
        {
            lblDateError.Visible = false;
        }

        protected void btnSelectChoicesComplete_Click(object sender, EventArgs e)
        {
            publishWizard.ActiveViewIndex = 4;
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void tbAddChoice_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbChoiceText.Text))
                lbChoices.Items.Add(tbChoiceText.Text);
        }

        protected void btnDeleteChoice_Click(object sender, EventArgs e)
        {
            if (lbChoices.SelectedIndex > -1)
                lbChoices.Items.RemoveAt(lbChoices.SelectedIndex);
        }

        protected void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (lbUsers.SelectedIndex > -1)
                lbUsers.Items.RemoveAt(lbUsers.SelectedIndex);
        }

        protected void GridView_Users_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "SelectUser")
            {
                int rowIdx = Convert.ToInt32(e.CommandArgument);
                Guid userId = (Guid)GridView_Users.DataKeys[rowIdx].Value;
                string userName = GridView_Users.Rows[rowIdx].Cells[0].Text;
                ListItem li = new ListItem(Server.HtmlDecode(userName), userId.ToString());
                if (!lbUsers.Items.Contains(li))
                    lbUsers.Items.Add(li);

            }
        }

        protected void btnSelectUsersContinue_Click(object sender, EventArgs e)
        {
            publishWizard.ActiveViewIndex = 4;
        }

        protected void publishWizard_ActiveViewChanged(object sender, EventArgs e)
        {

        }

       




    }
}
